package com.atguigu.utils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        //加到规定数才能执行要做的线程
        CyclicBarrier cyclicBarrier=new CyclicBarrier(9,new Thread(()->{
            System.out.println("集齐碎片");
        }));
        for (int i = 1; i <= 9; i++) {
            int a =i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"获得第"+a+"片碎片");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            },String.valueOf(i)).start();

        }
    }
}
